package com.atsmart.app.utils;

import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CacheManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.activity.ClientListActivity;
import com.atsmart.app.activity.MessageListActivity;
import com.atsmart.app.activity.OtherListActivity;
import com.atsmart.app.activity.ProductListActivity;
import com.atsmart.app.activity.ReportListActivity;
import com.atsmart.app.activity.TaskListActivity;
import com.atsmart.app.model.PositionShutDownInfo;
import com.atsmart.app.sql.ShutDownSqlHelper;

public class DialogUtil {
	private static DialogUtil dialogUtil = null;

	static {
		if (dialogUtil == null) {
			dialogUtil = new DialogUtil();
		}
	}

	public static void dialogWithErrorMsg(Context context, String errorString) {
		// AlertDialog.Builder builder = new Builder(context);
		// builder.setMessage(errorString)
		// .setTitle(context.getResources().getString(R.string.app_name))
		// .setPositiveButton(
		// context.getResources().getString(R.string.confirm),
		// new OnClickListener() {
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// }
		// }).show();
	}

	public static ProgressDialog getProgressDialog(Context context,
			String messageString) {
		return getProgressDialog(context, ProgressDialog.STYLE_SPINNER,
				messageString);
	}

	public static Dialog getLoadingDialog(Context context) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.setContentView(R.layout.loading_fullscreen);
		RelativeLayout view = (RelativeLayout) dialog.findViewById(R.id.dialog);
		view.setBackgroundResource(R.color.black);
		view.getBackground().setAlpha(120);
		return dialog;
	}

	public static Dialog getLoadingDialog(Context context, String mess) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.setContentView(R.layout.loading_fullscreen);
		RelativeLayout view = (RelativeLayout) dialog.findViewById(R.id.dialog);
		TextView message = (TextView) dialog.findViewById(R.id.message);
		message.setText(mess);
		view.setBackgroundResource(R.color.black);
		view.getBackground().setAlpha(120);
		return dialog;
	}

	public static ProgressDialog getProgressDialog(Context context, int style,
			String messageString) {
		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setProgressStyle(style);
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.setMessage(messageString);
		return dialog;
	}

	public static Dialog getLoadingDialogFullscreen(Context context) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		// dialog.setContentView(R.layout.loading_fullscreen);
		// dialog.setCancelable(false);
		// LinearLayout view = (LinearLayout) dialog.findViewById(R.id.dialog);
		// view.setGravity(Gravity.CENTER | Gravity.TOP);
		// view.setPadding(0, 200, 0, 0);
		return dialog;
	}

	public static AlertDialog getAlertDialog(Activity currentActivity,
			String title, String messageString, String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonListener,
			CharSequence negativeButton,
			DialogInterface.OnClickListener negativeButtonListener) {
		return getAlertDialog(currentActivity, title, messageString,
				positiveButtonText, positiveButtonListener, negativeButton,
				negativeButtonListener, true, true);
	}

	private static AlertDialog getAlertDialog(Activity currentActivity,
			String title, String messageString, String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonListener,
			CharSequence negativeButton,
			DialogInterface.OnClickListener negativeButtonListener,
			boolean isUsePositiveButton, boolean isUseNegativeButton) {
		AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
		if (TextUtils.isEmpty(title)) {
			builder.setTitle(R.string.app_name);
		} else {
			builder.setTitle(title);
		}
		if (!TextUtils.isEmpty(messageString)) {
			builder.setMessage(messageString).setCancelable(false);
		}
		if (isUsePositiveButton) {
			builder.setPositiveButton(positiveButtonText,
					positiveButtonListener);
		}
		if (isUseNegativeButton) {
			builder.setNegativeButton(negativeButton, negativeButtonListener);
		}
		return builder.create();
	}

	public static AlertDialog getExitAppDialog(final Activity activity) {
		AlertDialog dialog = new AlertDialog.Builder(activity)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.alert_title)
				.setMessage(R.string.confirm_exit)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								deleImgUrl();
								deletefile();
								 
								PrefsUtils.setIsNoti("1");
								BizApp.getInstance().setExited(true);
								affirmExitApp(activity, dialog);
							}

						}).setNegativeButton(R.string.no, null).create();
		return dialog;
	}

	/***删除图片记录*/
	public static void RecursionDeleteFile(File file) {
		if (file.exists()) {// 判断文件是否存在
			if (file.isFile()) { // 判断是否是文件
				file.delete();
			}
			if (file.isDirectory()) {// 否则如果它是一个目录
				File[] childFile = file.listFiles();// 声明目录下所有的文件 files[]
				if (childFile == null || childFile.length == 0) {
					file.delete();
					 
				}
				for (File f : childFile) {// 遍历目录下所有的文件
					RecursionDeleteFile(f);
				}
				file.delete();
			}
		}
	}
	private static void deletefile(){
		String path = BizApp.getInstance().getFilePath();
		File file = new File(path);
		File[] files = file.listFiles();
		for(File f: files){
			if(!f.getName().equals("inspection") && !f.getName().equals("report")){
				RecursionDeleteFile(f);
			}
		}
	}
	
	
	private static void deleImgUrl(){
		List<String> checkImg = checkImgUrl();
		File file = new File(BizApp.getInstance().getFilePath() + "/" + "report");
		File[] files = file.listFiles();
		if(files != null && files.length > 0){
			for(int i=0; i<files.length; i++){
				if(checkImg != null && checkImg.size() > 0){
					boolean bo = false;
					 for(int j=0; j<checkImg.size(); j++){
						 String fie = files[i].getAbsolutePath().trim();
						 if((fie).equals(checkImg.get(j).trim())){
							 bo = true;
							break;
						 } 
					 }
					 if(!bo){
						if(files[i].exists()){
//							Log.e("TAG", "图片文件删除 files[i].getName()： = " + files[i].getName() + " , files[i].getAbsolutePath(): = " + files[i].getAbsolutePath() + " , files[i].getPath(): = " + files[i].getPath());
							files[i].delete();
						}
					 }
				}else{
					if(files[i].exists()){
//						Log.e("TAGs", "图片文件删除 files[i].getName()： = " + files[i].getName() + " , files[i].getAbsolutePath(): = " + files[i].getAbsolutePath() + " , files[i].getPath(): = " + files[i].getPath());
						files[i].delete();
					}
				}
			}
		}
	}
	 
	private static List<String> checkImgUrl(){
		List<String> imageUrl = new ArrayList<String>();
		ShutDownSqlHelper psql = new ShutDownSqlHelper();
		psql.open(BizApp.getInstance().getSqlHelper());
		List<PositionShutDownInfo> repos = null;
		repos = psql.getAttributePosLit();
		if (repos != null && repos.size() > 0) {
			
			for (int i = 0; i < repos.size(); i++) {
				PositionShutDownInfo rep = repos.get(i);
				if (rep.getType() == 8) {
					String imagestr = rep.getImagestr();
					String[] strs = imagestr.split(";");
					for(String str: strs){
						String[] s = str.split(",");
						if(!StringUtil.isEmptyOrNull(s[1])){
							imageUrl.add(s[1]);
						}
					}
				}
			}
		}
		return imageUrl;
	}
	private static void affirmExitApp(final Activity activity,
			DialogInterface dialog) {
		dialog.dismiss();

		if (((activity.getClass() == TaskListActivity.class
				|| activity.getClass() == ReportListActivity.class || activity
				.getClass() == ClientListActivity.class) || activity.getClass() == MessageListActivity.class)
				|| (activity.getClass() == OtherListActivity.class)
				|| activity.getClass() == ProductListActivity.class) {
			activity.finish();
		} else {
			Intent intent = new Intent();
			intent.setClass(activity, TaskListActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			activity.finish();
			activity.startActivity(intent);
		}
	}

	public static void showLocationSetting(final Context context) {
		new AlertDialog.Builder(context)
				.setTitle(R.string.alert_title)
				.setMessage(R.string.setting_network_tips)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								context.startActivity(new Intent(
										Settings.ACTION_LOCATION_SOURCE_SETTINGS));
							}
						}).setNegativeButton(R.string.no, null).create().show();
	}

}
